﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using ST.Buz.Base;
using ST.Buz.Base.Entity;
using ST.Common.Util;
using ST.Common.Util.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ST.Buz.Service
{
	public partial interface IServiceFinanceRecvDetail : IBaseService<int, IMS_FinanceRecvDetail>
	{

	}

	public partial class ServiceFinanceRecvDetail : BaseService, IServiceFinanceRecvDetail
	{
		private readonly BuzContext context;
		private readonly ILogger logger;

		#region 基础方法
		/// <summary>
		/// 新增
		/// </summary>
		/// <param name="model"></param>
		/// <exception cref="System.ArgumentException">参数有误时抛出</exception>
		/// <exception cref="ST.Common.Util.BuzException">其他错误时抛出</exception>
		/// <returns></returns>
		public int Add(IMS_FinanceRecvDetail model)
		{
			try
			{
				if (model == null)
				{
					throw new ArgumentException("model");
				}
				context.FinanceRecvDetail.Add(model);
				context.SaveChanges();

				return model.ID;
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (DbUpdateException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw new BuzException(ex.Message, ex);
			}
			catch (BuzException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw new BuzException(ex.Message, ex);
			}
		}

		/// <summary>
		/// 修改
		/// </summary>
		/// <param name="model"></param>
		/// <exception cref="System.ArgumentNullException">参数有误时抛出</exception>
		/// <exception cref="ST.Common.Util.BuzException">其他错误时抛出</exception>
		/// <returns></returns>
		public bool Update(IMS_FinanceRecvDetail model)
		{
			try
			{
				if (model == null)
				{
					throw new ArgumentNullException("model");
				}
				var entity = context.FinanceRecvDetail.Attach(model);
				string[] unchanged = new string[] { "ID", "AddDate", "AddManID", "AddManName" };//排除不需要更新的属性名，非数据库列名
				foreach (var prop in entity.Properties)
				{
					if (!unchanged.Contains(prop.Metadata.Name))
					{
						prop.IsModified = true;
					}
					else
					{
						prop.IsModified = false;
					}
				}
				context.SaveChanges();
				return true;
			}
			catch (ArgumentNullException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (DbUpdateException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw new BuzException(ex.Message, ex);
			}
			catch (BuzException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw new BuzException(ex.Message, ex);
			}
		}

		/// <summary>
		/// 修改
		/// </summary>
		/// <param name="model"></param>
		/// <exception cref="System.ArgumentNullException">参数有误时抛出</exception>
		/// <exception cref="ST.Common.Util.BuzException">其他错误时抛出</exception>
		/// <returns></returns>
		public bool Update(IMS_FinanceRecvDetail model, string[] props)
		{
			try
			{
				if (props == null || props.Length == 0)
				{
					throw new ArgumentNullException("要更新的属性");
				}
				if (model == null)
				{
					throw new ArgumentNullException("model");
				}
				var entity = context.FinanceRecvDetail.Attach(model);
				foreach (var prop in entity.Properties)
				{
					if (props.Contains(prop.Metadata.Name))
					{
						prop.IsModified = true;
					}
					else
					{
						prop.IsModified = false;
					}
				}
				context.SaveChanges();
				return true;
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (DbUpdateException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw new BuzException(ex.Message, ex);
			}
			catch (BuzException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw new BuzException(ex.Message, ex);
			}
		}

		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="id"></param>
		/// <exception cref="System.ArgumentException">参数有误时抛出</exception>
		/// <exception cref="ST.Common.Util.EntityNotFoundException">未找到要操作的记录时抛出</exception>
		/// <exception cref="ST.Common.Util.BuzException">其他错误时抛出</exception>
		/// <returns></returns>
		public bool Delete(int id)
		{
			bool success = false;
			try
			{
				if (id <= 0)
				{
					throw new ArgumentException("id不能为空");
				}
				IMS_FinanceRecvDetail modelOld = context.FinanceRecvDetail.SingleOrDefault(m => m.ID == id);
				if (modelOld == null)
				{
					throw new EntityNotFoundException("未找到id为" + id + "的记录或记录已删除");
				}
				context.FinanceRecvDetail.Remove(modelOld);
				context.SaveChanges();

				success = true;
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (EntityNotFoundException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (DbUpdateException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw new BuzException(ex.Message, ex);
			}
			catch (BuzException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw new BuzException(ex.Message, ex);
			}
			return success;
		}

		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="model"></param>
		/// <exception cref="System.ArgumentException">参数有误时抛出</exception>
		/// <exception cref="ST.Common.Util.EntityNotFoundException">未找到要操作的记录时抛出</exception>
		/// <exception cref="ST.Common.Util.BuzException">其他错误时抛出</exception>
		/// <returns></returns>
		public bool Delete(IMS_FinanceRecvDetail model)
		{
			try
			{
				if (model == null)
				{
					throw new ArgumentException("model");
				}
				var entity = context.Entry(model);
				//var entity = context.FinanceRecvDetail.Attach(model);
				entity.State = EntityState.Deleted;
				context.SaveChanges();

				return true;
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (EntityNotFoundException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (DbUpdateException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw new BuzException(ex.Message, ex);
			}
			catch (BuzException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw new BuzException(ex.Message, ex);
			}
		}

		/// <summary>
		/// 是否存在
		/// </summary>
		/// <param name="id"></param>
		/// <exception cref="System.ArgumentException">参数有误时抛出</exception>
		/// <exception cref="ST.Common.Util.BuzException">其他错误时抛出</exception>
		/// <returns></returns>
		public bool Exists(int id)
		{
			try
			{
				if (id <= 0)
				{
					throw new ArgumentException("id不能为空");
				}
				return context.FinanceRecvDetail.Any(e => e.ID == id);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (BuzException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw new BuzException(ex.Message, ex);
			}
		}

		/// <summary>
		/// 是否存在
		/// </summary>
		/// <param name="model"></param>
		/// <exception cref="System.ArgumentException">参数有误时抛出</exception>
		/// <exception cref="ST.Common.Util.BuzException">其他错误时抛出</exception>
		/// <returns></returns>
		public bool Exists(IMS_FinanceRecvDetail model)
		{
			if (model == null)
			{
				throw new ArgumentException("model");
			}
			return Exists(model.ID);
		}

		/// <summary>
		/// 获取信息
		/// </summary>
		/// <param name="id"></param>
		/// <exception cref="System.ArgumentException">参数有误时抛出</exception>
		/// <exception cref="ST.Common.Util.EntityNotFoundException">未找到要操作的记录时抛出</exception>
		/// <exception cref="ST.Common.Util.BuzException">其他错误时抛出</exception>
		/// <returns></returns>
		public IMS_FinanceRecvDetail Get(int id, bool deattached = false)
		{
			try
			{
				if (id <= 0)
				{
					throw new ArgumentException("id不能为空");
				}
				var model = context.FinanceRecvDetail.SingleOrDefault(m => m.ID == id);
				if (model == null)
				{
					throw new EntityNotFoundException("未找到id为" + id + "的记录或记录已删除");
				}
				if (deattached)
				{
					var entity = context.Entry(model);
					entity.State = EntityState.Detached;
				}
				return model;
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (EntityNotFoundException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (BuzException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw new BuzException(ex.Message, ex);
			}
		}

		/// <summary>
		/// 获取分页列表
		/// </summary>
		/// <param name="pageNum"></param>
		/// <param name="pageSize"></param>
		/// <param name="sortOrder">为空则默认倒序排列</param>
		/// <exception cref="System.ArgumentException">参数有误时抛出</exception>
		/// <exception cref="ST.Common.Util.BuzException">其他错误时抛出</exception>
		/// <returns></returns>
		public PageData GetPageList(int pageNum, int pageSize, string sortOrder)
		{
			PageData result = null;
			try
			{
				if (pageNum < 1)
				{
					throw new ArgumentException("页码不能为空");
				}
				if (pageSize < 1)
				{
					throw new ArgumentException("每页显示条数不能为空");
				}
				if (string.IsNullOrEmpty(sortOrder))
				{
					sortOrder = "ID_desc";
				}

				bool descending = false;
				if (sortOrder.EndsWith("_desc", StringComparison.OrdinalIgnoreCase))
				{
					sortOrder = sortOrder.Substring(0, sortOrder.Length - 5);
					descending = true;
				}
				else if (sortOrder.EndsWith("_asc", StringComparison.OrdinalIgnoreCase))
				{
					sortOrder = sortOrder.Substring(0, sortOrder.Length - 4);
				}

				List<IMS_FinanceRecvDetail> lst = null;
				var query = from m in context.FinanceRecvDetail select m;

				if (descending)
				{
					//数据库分页，不支持多表
					query = query.OrderByDescending(e => EF.Property<object>(e, sortOrder));
					//内存分页
					//query = query.OrderByDescending(e => e.GetType().GetProperty(sortOrder).GetValue(e, null));
				}
				else
				{
					//数据库分页，不支持多表
					query = query.OrderBy(e => EF.Property<object>(e, sortOrder));
					//内存分页
					//query = query.OrderBy(e => e.GetType().GetProperty(sortOrder).GetValue(e, null));
				}
				if (pageNum > 1)
				{
					query = query.Skip(pageSize * (pageNum - 1));
				}

				lst = query.Take(pageSize).AsNoTracking().ToList();
				int total = query.Count();
				result = new PageData
				{
					list = lst,
					pnum = pageNum,
					psize = pageSize,
					total = total
				};
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (BuzException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw ex;
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				throw new BuzException(ex.Message, ex);
			}
			return result;
		}
		#endregion
	}
}